﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using NLog.Internal;
using NLog;
using NLog.Targets;
using System.IO;

namespace QLTTTH
{
    public partial class frmLogViewer : DevExpress.XtraEditors.XtraForm
    {
        public frmLogViewer()
        {
            InitializeComponent();
        }

        private void frmLogViewer_Load(object sender, EventArgs e)
        {
            lvLog.Columns[0].ImageIndex = 0;
            lvLog.Columns[1].ImageIndex = 1;
            lvLog.Columns[2].ImageIndex = 2;
            lvLog.Columns[3].ImageIndex = 3;
            lvLog.Columns[4].ImageIndex = 4;
            LoadListView();
        }

        private void LoadListView()
        {
            lvLog.Items.Clear();
            
            var fileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\QLTTTH\Logs\Info.log";
            ReadLogFile(fileName);

            fileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\QLTTTH\Logs\Error.log";
            ReadLogFile(fileName);
        }

        private void ReadLogFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return;
            }

            using (StreamReader file = new StreamReader(fileName))
            {
                string line;
                int imageindex;
                int itemcount = lvLog.Items.Count;

                while ((line = file.ReadLine()) != null)
                {
                    try
                    {
                        // ngay
                        string ngay = DateTime.Parse(line.Substring(0, line.IndexOf(' '))).ToShortDateString();
                        line = line.Substring(line.IndexOf(' ') + 1);
                        // giờ
                        string gio = DateTime.Parse(line.Substring(0, 8)).ToShortTimeString();
                        line = line.Substring(line.IndexOf(' ') + 1);
                        // tac dong
                        string tacdong = line.Substring(0, line.IndexOf(' '));
                        line = line.Substring(line.IndexOf('-') + 2);
                        // thao tao
                        string ac = line;

                        ListViewItem item = new ListViewItem();
                        item.SubItems.Add(ngay);
                        item.SubItems.Add(gio);
                        item.SubItems.Add(tacdong);
                        item.SubItems.Add(ac);

                        imageindex = GetImageIndex(ac);
                        lvLog.Items.Add(item);
                        lvLog.Items[itemcount].ImageIndex = imageindex;
                        itemcount++;
                    }
                    catch
                    {
                        return;
                    }
                }
            }
        }

        private int GetImageIndex(string ac)
        {
            if (ac.Contains("xem chi tiết"))
                return 5;
            else if (ac.Contains("thêm"))
                return 6;
            else if (ac.Contains("xóa"))
                return 7;
            else if (ac.Contains("cập nhật"))
                return 8;
            else if (ac.Contains("đăng nhập"))
                return 9;
            else if (ac.Contains("đăng xuất"))
                return 10;
            else if (ac.Contains("tìm kiếm"))
                return 11;
            else if (ac.Contains("Exception"))
                return 12;
            return 12;
        }
    }
}